package com.iqiyi.pps.epg.core.dao.rtmp;

import com.iqiyi.pps.epg.core.dao.base.BaseDao;
import com.iqiyi.pps.epg.core.model.rtmp.RtmpServerStat;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RtmpServerStatDao extends BaseDao<RtmpServerStat> {

    private static final String QUERY_RTMPSERVERSTAT_BY_STREAMNAME = "select r from RtmpServerStat r where r.id > 0 and r.active = 2 and r.status = 2 and r.name like ?";

    public List<RtmpServerStat> streamInfoSuggest (String streamName) {
        return createQuery(QUERY_RTMPSERVERSTAT_BY_STREAMNAME, streamName).list();
    }

    public List<RtmpServerStat> getListByName(String name) {
        String sql = "SELECT stat.`id`, stat.`server_id`, stat.`name`, stat.`reconn`, stat.`in_bytes`, stat.`out_bytes`, stat.`input_bits`, stat.`output_bits`, stat.`width`, stat.`height`, stat.`framerate`, stat.`bitrate`, stat.`video`, stat.`audio`, stat.`active`, stat.`uptime`, stat.`status`, stat.`create_time`, stat.`modify_time`, server.public_ip as address from rtmp_server_stat stat left join rtmp_server server on server.id = stat.server_id where stat.name like :name and stat.status = :status and stat.active = :active";
        return getSession().createSQLQuery(sql)
            .addEntity(RtmpServerStat.class)
            .setParameter("name", name + "%")
            .setParameter("status", RtmpServerStat.STATUS_VALID)
            .setParameter("active", RtmpServerStat.ACTIVE_VALID)
            .list();
    }
}
